<?php
/**
 * @author Inhere
 * @version v1.0
 * Use : 数据库操作管理服务
 * Date : 2015-1-10
 * DatabaseServiceProvider.php
 */
namespace ulue\base\providers;

use ulue\core\ioc\Container,
    ulue\core\db\DriverFactory;

class DatabaseServiceProvider extends AbstractConfigurableServiceProvider
{
    protected $serviceId  = 'db';

    /**
     * @inherit
     * @var array
     */
    protected $idAliases = [ 'system.database', 'database', 'app.db'];

    /**
     * 注册一个服务提者， DatabaseServiceProvider
     * @param  Container $container 传入容器
     * @return mixed|void
     */
    public function register(Container $container)
    {
        $config  = $this->options;

        $closure = function($options=[], $reConnect=false) use ($config)
        {
            $options = (is_array($options) && $options) ? $options :$config;

            if ( empty($options['driver']) ) {
                $options['driver'] = 'mysql';
            }

            return DriverFactory::getDriver($options['driver'], $options, $reConnect);
        };

        $container->set($this->serviceId,$closure)->aliases($this->idAliases);
    }
}